(function(){

var map = Default.map;
var $ = Go.$;

function StarTab(isActive){
  this.markers = {
    medal:{},
    event:{},
    venue:{}
  };
  if(isActive) this.active();
}
window.StarTab = StarTab;
StarTab.prototype = new GoTab(Default.key.star, null, true);
StarTab.prototype.load = function(){
  this.loadCountry();
  this.loadEvent();
  this.loadVenue();
  
  this.curKey = Default.key.medal;
  this.showMarkers(this.curKey);
  this.checkMapTypeButton(this.curKey);
}
StarTab.prototype.reload = function(){
  this.load();
}
StarTab.prototype.clear = function(){
  this.hideMarkers(this.curKey);
}
StarTab.prototype.loadCountry = function(){
  this.markers.medal = {};
  var pane = $("medalbrief2"); pane.innerHTML = "";
  var key = Default.key.medal;
  var medalTab = Default.tabs[key];
  var favos = Go.getFavo(key);
  
//  refreshStarredMedalTable(pane, favos);  //TODO
}
StarTab.prototype.loadEvent = function(){
  this.markers.event = {};
  var pane = $("eventbrief2"); pane.innerHTML = "";
  var key = Default.key.event;
  var eventTab = Default.tabs[key];
  var favos = Go.getFavo(key);
  if(favos.length == 0){
  	this.addNothingHint(pane, key);
  } else {
  	var mgr = VenueMgr.getInstance();
    for(var i = 0, id; id = favos[i]; i++){
      var event = eventTab.getEventById(id); 
      if(event){
        var node = eventTab.createEventNode(id, event, pane, true, mgr);
        var sn = mgr.getMappingKey(event.venue);
        event.sn = sn;
        if(!this.markers.event[sn]) {
          this.markers.event[sn] = sn;
        }
        GEvent.addDomListener(node, "click", GEvent.callback(this, this.refreshMap));
      }
    }
  }
}
StarTab.prototype.loadVenue = function(){
  this.markers.venue = {};
  var pane = $("venuebrief2"); pane.innerHTML = "";
  var key = Default.key.venue;
  var venueTab = Default.tabs[key];
  var favos = Go.getFavo(key);
  if(favos.length == 0){
  	this.addNothingHint(pane, key);
  } else {
  	var mgr = VenueMgr.getInstance();
    for(var i = 0, sn; sn = favos[i]; i++){
      var venue = DVenue.item[sn];
      var node = venueTab.createVenueNode(sn, venue, pane, true, mgr);
      if(!this.markers.venue[sn]){
        this.markers.venue[sn] = sn;
      }
      GEvent.addDomListener(node, "click", GEvent.callback(this, this.refreshMap));
    }
  }
}
StarTab.prototype.openInfowindow = function(sn, nodeId){

}
StarTab.prototype.refreshMap = function(e){
  var e = e || window.event;
  var node = e.target || e.srcElement;
  var result = node.id.split("_");
  var key = result[1];
  if(key != this.curKey){
  	this.hideMarkers(this.curKey);
    this.showMarkers(key);
    this.checkMapTypeButton(key);  
    this.curKey = key;
  }
}
//StarTab.prototype.removeMarker = function(id, key){
//  var marker = MyActive.markers[key][id];
//  if(marker){
//  	Default.map.removeOverlay(marker);
//  }
//}
StarTab.prototype.addNothingHint = function(pane, key){
  Go.$n("span", pane, null, "Nothing yet.");
  var link = Go.$n("a", pane, null, "+Add to favorites", {href:"#"});
  GEvent.addDomListener(link, "click", function(){
    Default.tabs[key].active();
  });
}
StarTab.prototype.hideMarkers = function(key){
  for(var i in this.markers[key]){
    var sn = this.markers[key][i];
    DVenue.item[sn].marker.hide();
  }
}
StarTab.prototype.showMarkers = function(key){
  for(var i in this.markers[key]){
    var sn = this.markers[key][i];
    DVenue.item[sn].marker.show();
  }
  this.resizeMap(this.markers[key]);
}
})();
Default.tabs[Default.key.star] = new StarTab();
